<?php
/*
    ID:   $Id: $
    Name: John Griffin
    Date: May 26,2009
*/
$dir = preg_replace ('/\/trunk\/.*/', '/trunk', __FILE__);
ini_set ('include_path', $dir.'/source:'.$dir.'/test:'.ini_get ('include_path'));

require_once 'simpletest/autorun.php';
require_once 'lib/Error.php';

class TestError extends UnitTestCase {
    private $cwd = '';
    
    public function __construct () {
        parent::__construct ();
        $this->cwd = dirname (__FILE__);
    }
    
    private function fileGetContents ($filename, $contents) {
        $path = $this->cwd.'/'.$filename;
        if (file_exists ($path) == true) {
            $return = file_get_contents ($path);
        } else {
            file_put_contents ($path, $contents);
            chmod ($path, 0666);
            $return = $contents;
        }
        return $return;
    }

    public function testInvoke () {
        $error = new Error ();
        $this->assertTrue (true);
    }
    
    public function testAddError1 () {
        $error = new Error ();
        $error->addError (Error::NOT_IMPLEMENTED, 'test_field', 'test_message');
        $actual = print_r ($error, true);
        $expected = $this->fileGetContents ('test_Error/testAddError1.txt', $actual);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    public function testAddError2 () {
        $error = new Error ();
        $error->addError (Error::NOT_IMPLEMENTED, 'test_field1', 'test_message1');
        $error->addError (Error::REQUIRED_FIELD_MISSING, 'test_field2', 'test_message2');
        $actual = print_r ($error, true);
        $expected = $this->fileGetContents ('test_Error/testAddError2.txt', $actual);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    public function testAddError3 () {
        $error = new Error ();
        $error->addError (Error::NOT_IMPLEMENTED, 'test_field1', 'test_message1');
        $error->addError (Error::REQUIRED_FIELD_MISSING, 'test_field2', 'test_message2');
        $result = array ();
        foreach ($error as $key => $value) {
            $result[] = $value;
        }
        $actual = print_r ($result, true);
        $expected = $this->fileGetContents ('test_Error/testAddError3.txt', $actual);
        $this->assertTrue (strcmp ($expected, $actual) == 0);
    }
    
    public function testCountInterface1 () {
        $error = new Error ();
        $expected = 0;
        $actual = count ($error);
        $this->assertTrue ($actual == $expected);
    }
    
    public function testCountInterface2 () {
        $error = new Error ();
        $error->addError (Error::NOT_IMPLEMENTED, 'test_field1', 'test_message1');
        $expected = 1;
        $actual = count ($error);
        $this->assertTrue ($actual == $expected);
    }
}
?>

